package com.example.ordersystem;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.ordersystem.pojo.Food;


import com.example.ordersystem.pojo.Order;
import com.example.ordersystem.pojo.Store;
import com.example.ordersystem.utils.ListDataSave;
import com.example.ordersystem.utils.MyDBOpenHelper;

import com.example.ordersystem.utils.OrderFoodAdapter;
import com.example.ordersystem.utils.Order_Food;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/*
 * 订单页面
 */
public class OrderFoodFragment extends Fragment {

    private int money;
    private TextView totalMoney;
    private TextView payMoney;
    private TextView cleanOrder;

    private SQLiteDatabase db;
    private MyDBOpenHelper myDBHelper;

    private List<Food> order_food = new ArrayList<>();
    private HashMap<String, List<Food>> map = new HashMap<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.order_food, container, false);
        myDBHelper = new MyDBOpenHelper(getContext());
        String username = ListDataSave.getUserLogin(getContext());

        if (!username.equals("LoginOut")) {

            //简单存储
            order_food = Order_Food.getOrder_Food();

            //用map集合存储用户和用户所喜爱的菜品
            map.put(username, order_food);

            ListDataSave.putUserFoodOrder(getContext(), map, username);//存

            HashMap<String, List<Food>> userFoodOrderList = ListDataSave.getUserFoodOrderList(getContext(), username);

            List<Food> list = userFoodOrderList.get(username);

            OrderFoodAdapter orderFoodAdapter = new OrderFoodAdapter(getActivity(), R.layout.order_food_list, list);

            ListView listView = view.findViewById(R.id.order_list_view);

            listView.setAdapter(orderFoodAdapter);

        }
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        totalMoney = getActivity().findViewById(R.id.textView_totalMoney);
        payMoney = getActivity().findViewById(R.id.textView_payOrder);
        cleanOrder = getActivity().findViewById(R.id.textView_clean);

        final String userLogin = ListDataSave.getUserLogin(getContext());

        if (!userLogin.equals("LoginOut")) {
            money = 0;
            for (Food food : ListDataSave.getUserFoodOrderList(getContext(), userLogin).get(userLogin)) {
                money += food.getPrice() * food.getNum();
            }
            String s = "总金额：" + money + "元";
            totalMoney.setText(s);

            payMoney.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    List<Food> order_food = Order_Food.getOrder_Food();

                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date();
                    String time = df.format(date);
                    db = myDBHelper.getReadableDatabase();
                    //数据库保存order_food
                        for (Food food : order_food) {

                            String storeName = null;
                            List<Store> storeList = ListDataSave.getStoreList(getContext());
                            for (Store store : storeList) {
                                if(food.getStoreId()==store.getStoreId()){
                                    storeName=store.getStoreName();
                                }
                            }

                            ContentValues values1 = new ContentValues();
                            values1.put("foodName",food.getFoodName());
                            values1.put("foodMessage",food.getMessage());
                            values1.put("foodPrice",(food.getPrice()*food.getNum()));
                            values1.put("foodNum",food.getNum());
                            values1.put("storeName",storeName);
                            values1.put("userName",userLogin);
                            values1.put("otime",time);
                            db.insert("user_order",null,values1);
                        }

                    order_food.clear();
                    Toast.makeText(getContext(), "已经结算订单共" + money + "元 " + "订单已进入历史订单", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getContext(), MainActivity.class);
                    boolean order = true;
                    intent.putExtra("order", order);
                    getContext().startActivity(intent);

                }
            });

            cleanOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    List<Food> order_food = Order_Food.getOrder_Food();

                    order_food.clear();

                    Toast.makeText(getContext(), "已经清空订单", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getContext(), MainActivity.class);
                    boolean order = true;
                    intent.putExtra("order", order);
                    getContext().startActivity(intent);
                }
            });
        }
    }

}
